package jp.co.hcs.attendancemanagement.dialog.action;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jp.co.hcs.attendancemanagement.action.AMAction;
import jp.co.hcs.attendancemanagement.dialog.logic.ProjectSearchLogic;
import jp.co.hcs.attendancemanagement.dialog.logic.dto.ProjectDTO;
import net.sf.json.JSONArray;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class ProjectSearchAction extends AMAction {

  public ActionForward executeImpl(ActionMapping mapping, ActionForm form,
      HttpServletRequest request, HttpServletResponse response) throws Exception {

    String dept = request.getParameter("department");

    if (dept == null) {
      return null;
    }

    response.setContentType("text/json");

    ProjectSearchLogic logic = new ProjectSearchLogic();
    List<ProjectDTO> listProjects = logic.searchProject(dept);
    JSONArray jsonArray = JSONArray.fromObject(listProjects);

    PrintWriter pw = response.getWriter();
    pw.write("{\"listProjects\":" + jsonArray.toString() + "}");
    pw.flush();
    pw.close();

    return null;
  }

}
